/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.globussoft.util;

/**
 *
 * @author Globussoft
 */
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MacAddress {
    public static String getMacAddress()
    {
       String macAdd="";
        try {
            //InetAddress address = InetAddress.getLocalHost();
            InetAddress address = InetAddress.getByName(InetAddress.getLocalHost().getHostAddress());

            /*
             * Get NetworkInterface for the current host and then read the
             * hardware address.
             */
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            if (ni != null) {
                byte[] mac = ni.getHardwareAddress();
                //System.out.println(byteArrayToHexString(mac));
                macAdd=byteArrayToHexString(mac);
//                if (mac != null) {
//                    /*
//                     * Extract each array of mac address and convert it to hexa with the
//                     * following format 08-00-27-DC-4A-9E.
//                     */
////                    for (int i = 0; i < mac.length; i++) {
////
////                        //System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
////                    }
//                } else {
//                    System.out.println("Address doesn't exist or is not accessible.");
//                }
            }
            else {
                System.out.println("Network Interface for the specified address is not found.");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }
       return macAdd;
    }

static String byteArrayToHexString(byte in[]) {

    byte ch = 0x00;

    int i = 0;

    if (in == null || in.length <= 0)

        return null;



    String pseudo[] = {"0", "1", "2",
"3", "4", "5", "6", "7", "8",
"9", "A", "B", "C", "D", "E",
"F"};

    StringBuffer out = new StringBuffer(in.length * 2);



    while (i < in.length) {

        ch = (byte) (in[i] & 0xF0);


        ch = (byte) (ch >>> 4);
     // shift the bits down

        ch = (byte) (ch & 0x0F);
// must do this is high order bit is on!

        out.append(pseudo[ (int) ch]); // convert thenibble to a String Character

        ch = (byte) (in[i] & 0x0F); // Strip offlow nibble

        out.append(pseudo[ (int) ch]); // convert thenibble to a String Character

        i++;

    }

    String rslt = new String(out);

    return rslt;

}
    public static void main(String[] args) {
      System.out.println(new MacAddress().getMacAddress());  ;
      
    }
}
